// Decompiled by DJ v3.11.11.95 Copyright 2009 Atanas Neshkov  Date: 2016/5/3 16:07:53
// Home Page: http://members.fortunecity.com/neshkov/dj.html  http://www.neshkov.com/dj.html - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   BadgeView.java

package com.mediacloud.app.newsmodule.view;

import com.mediacloud.app.newsmodule.R;

import android.content.Context;
import android.graphics.*;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.util.*;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

import java.text.DecimalFormat;

public class BadgeView extends TextView
{
	public View container;

	public BadgeView(Context context)
	{
		this(context, null);
	}

	public BadgeView(Context context, AttributeSet attrs)
	{
		this(context, attrs, 0x1010084);
	}

	public BadgeView(Context context, AttributeSet attrs, int defStyle)
	{
		super(context, attrs, defStyle);
		mHideOnNull = true;
		init();
	}

	private void init()
	{
		if(!(getLayoutParams() instanceof android.widget.FrameLayout.LayoutParams))
		{
			android.widget.FrameLayout.LayoutParams layoutParams = new android.widget.FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.END);
			setLayoutParams(layoutParams);
		}
		setTextColor(-1);
		setTypeface(Typeface.DEFAULT_BOLD);
//		setTextSize(2, 8F);
		float value=getResources().getDimensionPixelSize(R.dimen.dime_eight);
		value=value/getResources().getDisplayMetrics().density+0.5f;
		setTextSize(value);
		int pixel1,pixel2,pixel3,pixel4;
		//		pixel1=getResources().getDimensionPixelSize(R.dimen.dime_five);
		//		pixel2=getResources().getDimensionPixelSize(R.dimen.dime_one);
		//		pixel3=getResources().getDimensionPixelSize(R.dimen.dime_five);
		//		pixel4=getResources().getDimensionPixelSize(R.dimen.dime_one);
		//		setPadding(pixel1, pixel2, pixel3, pixel4);
		setPaddingRelative(dip2Px(5f), dip2Px(1f), dip2Px(5f), dip2Px(1f));
		setBgColor();
		setGravity(17);
		setHideOnNull(true);
		setBadgeCount(0);
	}

	public void setBgColor()
	{
		setBgColor(Color.parseColor("#d3321b"));
	}

	public void setBgColor(int color)
	{
		setBackground(8f, color);
	}

	public void setBackground(float dipRadius, int badgeColor)
	{
		float radius = dip2Px(dipRadius);
		float radiusArray[] = {
                radius, radius, radius, radius, radius, radius, radius, radius
		};
		RoundRectShape roundRect = new RoundRectShape(radiusArray, null, null);
		ShapeDrawable bgDrawable = new ShapeDrawable(roundRect);
		bgDrawable.getPaint().setColor(badgeColor);
		setBackgroundDrawable(bgDrawable);
	}

	public boolean isHideOnNull()
	{
		return mHideOnNull;
	}

	public void setHideOnNull(boolean hideOnNull)
	{
		mHideOnNull = hideOnNull;
		setText(getText());
	}
	@Override
	public void setText(CharSequence text, android.widget.TextView.BufferType type)
	{
		try
		{
			text=formatStrValue(Integer.parseInt(text.toString()));
		}
		catch (Exception e)
		{
		}
		super.setText(text, type);
		if(isHideOnNull() && (text == null || text.toString().equalsIgnoreCase("0")))
			setVisibility(View.INVISIBLE);
		else
			setVisibility(View.VISIBLE);
	}

	public String formatStrValue(int num)
	{
		int max=getResources().getInteger(R.integer.tenthousands_commentnum);
		String tenthousandsStr=getResources().getString(R.string.tenthousands);
		DecimalFormat df = new DecimalFormat("#.#");
		if(num>=max)
		{
			double value=num/(max*1.0D);
			return df.format(value)+tenthousandsStr;
		}
//		else if(num>=(max/10))
//		{
//			double value=num/(max/10.0D);
//			return df.format(value)+thousandsStr;
//		}
		else if(num>=0)
			return num+"";
		return null;
	}

	public void setBadgeCount(long count)
	{
		setText(String.valueOf(count));
	}

	public Integer getBadgeCount()
	{
		if(getText() == null)
			return null;
		String text = getText().toString();
		try
		{
			return Integer.valueOf(Integer.parseInt(text));
		}
		catch(NumberFormatException e)
		{
			return null;
		}
	}

	public void setBadgeGravity(int gravity)
	{
		android.widget.FrameLayout.LayoutParams params = (android.widget.FrameLayout.LayoutParams)getLayoutParams();
		params.gravity = gravity;
		setLayoutParams(params);
	}


	public void setBadgeMargin(int leftDipMargin, int topDipMargin, int rightDipMargin, int bottomDipMargin)
	{
		android.widget.FrameLayout.LayoutParams params = (android.widget.FrameLayout.LayoutParams)getLayoutParams();
		params.setMarginStart(dip2Px(leftDipMargin));
		params.topMargin = dip2Px(topDipMargin);
		params.setMarginEnd(dip2Px(rightDipMargin));
		params.bottomMargin = dip2Px(bottomDipMargin);
		setLayoutParams(params);
	}

	public int[] getBadgeMargin()
	{
		android.widget.FrameLayout.LayoutParams params = (android.widget.FrameLayout.LayoutParams)getLayoutParams();
		return (new int[] {
				params.getMarginStart(), params.topMargin, params.getMarginEnd(), params.bottomMargin
		});
	}

	public void incrementBadgeCount(int increment)
	{
		Integer count = getBadgeCount();
		if(count == null)
			setBadgeCount(increment);
		else
			setBadgeCount(increment + count.intValue());
	}



	public void setTargetView(TabWidget target, int tabIndex)
	{
		View tabView = target.getChildTabViewAt(tabIndex);
		setTargetView(tabView);
	}

	public void setTargetView(View target)
	{
		if(getParent() != null)
			((ViewGroup)getParent()).removeView(this);
		if(target == null)
			return;
		if(target.getParent() instanceof FrameLayout)
			((FrameLayout)target.getParent()).addView(this);
		else
		if(target.getParent() instanceof ViewGroup)
		{
			ViewGroup parentContainer = (ViewGroup)target.getParent();
			int groupIndex = parentContainer.indexOfChild(target);
			parentContainer.removeView(target);
			FrameLayout badgeContainer = new FrameLayout(getContext());
			android.view.ViewGroup.LayoutParams parentlayoutParams = target.getLayoutParams();
			badgeContainer.setLayoutParams(parentlayoutParams);
			target.setLayoutParams(new android.view.ViewGroup.LayoutParams(-1, -1));
			parentContainer.addView(badgeContainer, groupIndex, parentlayoutParams);
			badgeContainer.addView(target);
			badgeContainer.addView(this);
			container=badgeContainer;
		} else
		if(target.getParent() == null)
			Log.e(getClass().getSimpleName(), "ParentView is needed");
	}

	@Override
	public void setVisibility(int visibility) {
		super.setVisibility(visibility);
//		if(container!=null)
//		{
//			container.setVisibility(visibility);
//		}
	}

	private int dip2Px(float dip)
	{
		return (int)(dip * getContext().getResources().getDisplayMetrics().density + 0.5F);
	}

	private boolean mHideOnNull;
}